﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace SIMcSDK
{
        
    // delegati pre callbacky .. SpravaPrijata rusim, nech je to vsetko jednotne v EN
    public delegate void OnMessageReceived(string from, string message);
    //public delegate void SpravaPrijata(string from, string message);
    public delegate void OnMessageWaiting(int mwi, StringBuilder info);
    

    public class IMControl
    {
        [DllImport("pjsipDll.dll")]
        private static extern int dll_sendMessage(int buddyId, string uri, string message);

        #region Callback declarations
        // Callback function registration declarations 
        // passing delegate to unmanaged code (.dll)
        [DllImport("pjsipDll.dll")]
        private static extern int onMessageReceivedCallback(OnMessageReceived cb);
        [DllImport("pjsipDll.dll")]
        private static extern int onMessageWaitingCallback(OnMessageWaiting cb);

        // Static declaration because of CallbackonCollectedDelegate exception! .. nastavim dany callback a v fcii initialize() ich passnem do .dll fcie
        static OnMessageReceived mrdel = new OnMessageReceived(onMessageReceived);
        static OnMessageWaiting mwidel = new OnMessageWaiting(onMessageWaiting);
               

        //  notifier na prichodziu spravu .. verejne pristupny, tu sa pridavaju fcie na handlovanie
        public static event OnMessageReceived MessageReceived;
        public static event OnMessageWaiting MessageWaiting;

        #endregion Callback declarations

        #region Callbacks
        //  tu checknem, ci je pridana nejaka fcia na spustenie do eventu, ak hej, tak ju vykonam
        private static void onMessageReceived(string from, string text)
        {
            if (null != MessageReceived) MessageReceived(from, text);
            return;
        }

        private static void onMessageWaiting(int mwi, StringBuilder info)
        {
            if (null != MessageWaiting) MessageWaiting(mwi, info);
            return;
        }
        #endregion

        /// <summary>
        /// Static initializer. Call this method to set callbacks from SIP stack. 
        /// </summary>
        public static void initialize()
        {
            // assign callbacks
            onMessageReceivedCallback(mrdel);
            onMessageWaitingCallback(mwidel);            
        }


        public static int sendMessage(string dest, string message)
        {
            return dll_sendMessage(2, dest, message);
        }
    }
}
